package hx.bean;

import hx.config.Global;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HX<titanrain@163.com>
 */
public class Query {

    private String queryNames;
    private String queryOps;
    private String queryValues;
    private StringBuilder where = new StringBuilder(" 1 = 1 ");
    private List<Object> params = new ArrayList<Object>();

    public Query() {

    }

    public Query(String queryNames, String queryOps, String queryValues) {
        if (queryNames != null) {
            this.queryNames = queryNames.trim();
            this.queryOps = queryOps.trim();
            this.queryValues = queryValues.trim();
        }
    }

    public String condition(String alias) {
        String tmpQueryNames = this.queryNames;
        String tmpQueryValues = this.queryValues;
        if (tmpQueryNames == null || "".equals(tmpQueryNames)) {
            return " 1 = 1 ";
        }
        String[] nameArr = tmpQueryNames.split(Global.paramSplit);
        String[] opArr = this.queryOps.split(Global.paramSplit);
        String[] valueArr = tmpQueryValues.split(Global.paramSplit);

        String tempAlias;
        if (alias != null && !alias.equals("")) {
            tempAlias = alias + ".";
        } else {
            tempAlias = "";
        }
        for (int i = 0; i < nameArr.length; i++) {
            where.append(" and ").append(tempAlias).append(nameArr[i]).append(" ");
            if ("like".equals(opArr[i])) {
                where.append(opArr[i]).append(" ");
                where.append(new StringBuilder("'%").append(valueArr[i]).append("%' ").toString());
            } else if ("in".equals(opArr[i])) {
                where.append(opArr[i]).append(" ");
                if (valueArr[i] != null && !valueArr[i].equals("")) {
                    String[] inValues = valueArr[i].split(",");
                    where.append("(");
                    for (String inVal : inValues) {
                        where.append("'").append(inVal).append("',");
                    }
                    where.deleteCharAt(where.length() - 1);
                    where.append(") ");
                }
            } else if ("eq".equals(opArr[i])) {
                where.append("=").append(" ");
                where.append(new StringBuilder("'").append(valueArr[i]).append("' ").toString());
            } else if ("gt".equals(opArr[i])) {
                where.append(">").append(" ");
                where.append(new StringBuilder("'").append(valueArr[i]).append("' ").toString());
            } else if ("lt".equals(opArr[i])) {
                where.append("<").append(" ");
                where.append(new StringBuilder("'").append(valueArr[i]).append("' ").toString());
            } else if ("ge".equals(opArr[i])) {
                where.append(">=").append(" ");
                where.append(new StringBuilder("'").append(valueArr[i]).append("' ").toString());
            } else if ("le".equals(opArr[i])) {
                where.append("<=").append(" ");
                where.append(new StringBuilder("'").append(valueArr[i]).append("' ").toString());
            } else if ("nq".equals(opArr[i])) {
                where.append("<>").append(" ");
                where.append(new StringBuilder("'").append(valueArr[i]).append("' ").toString());
            } else {
                where.append(opArr[i]).append(" ");
                where.append(new StringBuilder("'").append(valueArr[i]).append("' ").toString());
            }
        }
        return where.toString();
    }

    public List<Object> params() {
        return params;
    }

}
